<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片压缩工具 - 在线工具集合</title>
    <script src="../utils/toast.js"></script>
    <link rel="stylesheet" href="../utils/styles.css">
    <script src="https://cdn.jsdelivr.net/npm/browser-image-compression@2.0.2/dist/browser-image-compression.js"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
            min-height: 100vh;
            color: #fff;
            position: relative;
            overflow-x: hidden;
        }

        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: radial-gradient(circle at 50% 50%, rgba(255,255,255,0.1) 0%, transparent 50%);
            pointer-events: none;
            z-index: 0;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 40px 20px;
            position: relative;
            z-index: 2;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .header p {
            color: rgba(255, 255, 255, 0.8);
            font-size: 1.1em;
        }

        .compressor-section {
            background: rgba(255, 255, 255, 0.05);
            border-radius: 24px;
            padding: 40px;
            margin-bottom: 30px;
            border: 1px solid rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        }

        .upload-area {
            border: 2px dashed rgba(255, 255, 255, 0.2);
            border-radius: 20px;
            padding: 40px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 30px;
            background: rgba(255, 255, 255, 0.03);
        }

        .upload-area:hover {
            border-color: #00ff88;
            background: rgba(255, 255, 255, 0.05);
        }

        .upload-area.dragover {
            border-color: #00ff88;
            background: rgba(0, 255, 136, 0.1);
        }

        .upload-icon {
            font-size: 48px;
            margin-bottom: 20px;
            color: #00ff88;
        }

        .upload-text {
            color: rgba(255, 255, 255, 0.8);
            font-size: 1.1em;
            margin-bottom: 10px;
        }

        .upload-hint {
            color: rgba(255, 255, 255, 0.5);
            font-size: 0.9em;
        }

        .compression-options {
            background: rgba(255, 255, 255, 0.03);
            padding: 25px;
            border-radius: 20px;
            margin-bottom: 30px;
            border: 1px solid rgba(255, 255, 255, 0.05);
        }

        .option-group {
            margin-bottom: 20px;
        }

        .option-group:last-child {
            margin-bottom: 0;
        }

        .option-group label {
            display: block;
            margin-bottom: 10px;
            color: rgba(255, 255, 255, 0.9);
            font-size: 1em;
            font-weight: 500;
        }

        .option-group input[type="range"] {
            width: 100%;
            height: 6px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 3px;
            outline: none;
            -webkit-appearance: none;
        }

        .option-group input[type="range"]::-webkit-slider-thumb {
            -webkit-appearance: none;
            width: 20px;
            height: 20px;
            background: #00ff88;
            border-radius: 50%;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .option-group input[type="range"]::-webkit-slider-thumb:hover {
            transform: scale(1.1);
        }

        .option-value {
            display: inline-block;
            margin-left: 10px;
            color: #00ff88;
            font-weight: 500;
        }

        .preview-area {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .preview-box {
            background: rgba(255, 255, 255, 0.03);
            padding: 20px;
            border-radius: 16px;
            text-align: center;
            border: 1px solid rgba(255, 255, 255, 0.05);
        }

        .preview-box h3 {
            color: rgba(255, 255, 255, 0.9);
            margin-bottom: 15px;
            font-size: 1.1em;
        }

        .preview-image {
            max-width: 100%;
            max-height: 200px;
            border-radius: 8px;
            margin-bottom: 15px;
        }

        .image-info {
            color: rgba(255, 255, 255, 0.7);
            font-size: 0.9em;
            margin-bottom: 10px;
        }

        .compression-ratio {
            color: #00ff88;
            font-weight: 500;
        }

        .action-buttons {
            display: flex;
            gap: 20px;
            justify-content: center;
            margin-top: 30px;
        }

        .action-btn {
            padding: 15px 30px;
            border: none;
            border-radius: 12px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            color: #1a1a1a;
            font-size: 1.1em;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(0, 255, 136, 0.2);
            letter-spacing: 0.5px;
        }

        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(0, 255, 136, 0.3);
        }

        .action-btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .back-link {
            display: inline-block;
            margin-bottom: 20px;
            color: #00ff88;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .back-link:hover {
            transform: translateX(-5px);
        }

        @media (max-width: 768px) {
            .container {
                padding: 20px 10px;
            }

            .header h1 {
                font-size: 2em;
            }

            .action-buttons {
                flex-direction: column;
            }

            .action-btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="../index.html" class="back-link">← 返回首页</a>
        
        <div class="header">
            <h1>图片压缩工具</h1>
            <p>快速压缩图片，保持画质的同时减小文件大小</p>
        </div>

        <div class="compressor-section">
            <div class="upload-area" id="uploadArea">
                <div class="upload-icon">📁</div>
                <div class="upload-text">点击或拖拽图片到这里</div>
                <div class="upload-hint">支持 JPG、PNG、WebP 格式</div>
            </div>

            <div class="compression-options">
                <div class="option-group">
                    <label>压缩质量 <span class="option-value" id="qualityValue">0.8</span></label>
                    <input type="range" id="quality" min="0.1" max="1" step="0.1" value="0.8">
                </div>
                <div class="option-group">
                    <label>最大宽度 <span class="option-value" id="maxWidthValue">1920</span>px</label>
                    <input type="range" id="maxWidth" min="100" max="4000" step="100" value="1920">
                </div>
                <div class="option-group">
                    <label>最大高度 <span class="option-value" id="maxHeightValue">1080</span>px</label>
                    <input type="range" id="maxHeight" min="100" max="4000" step="100" value="1080">
                </div>
            </div>

            <div class="preview-area" id="previewArea"></div>

            <div class="action-buttons">
                <button class="action-btn" id="compressBtn" disabled>开始压缩</button>
                <button class="action-btn" id="downloadBtn" disabled>下载压缩图片</button>
            </div>
        </div>
    </div>

    <script>
        const uploadArea = document.getElementById('uploadArea');
        const previewArea = document.getElementById('previewArea');
        const compressBtn = document.getElementById('compressBtn');
        const downloadBtn = document.getElementById('downloadBtn');
        const qualityInput = document.getElementById('quality');
        const maxWidthInput = document.getElementById('maxWidth');
        const maxHeightInput = document.getElementById('maxHeight');
        const qualityValue = document.getElementById('qualityValue');
        const maxWidthValue = document.getElementById('maxWidthValue');
        const maxHeightValue = document.getElementById('maxHeightValue');

        let originalFile = null;
        let compressedFile = null;

        // 更新显示值
        qualityInput.addEventListener('input', () => {
            qualityValue.textContent = qualityInput.value;
        });

        maxWidthInput.addEventListener('input', () => {
            maxWidthValue.textContent = maxWidthInput.value;
        });

        maxHeightInput.addEventListener('input', () => {
            maxHeightValue.textContent = maxHeightInput.value;
        });

        // 拖拽上传
        uploadArea.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadArea.classList.add('dragover');
        });

        uploadArea.addEventListener('dragleave', () => {
            uploadArea.classList.remove('dragover');
        });

        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
            const file = e.dataTransfer.files[0];
            if (file && file.type.startsWith('image/')) {
                handleFile(file);
            } else {
                toast.error('请上传图片文件');
            }
        });

        // 点击上传
        uploadArea.addEventListener('click', () => {
            const input = document.createElement('input');
            input.type = 'file';
            input.accept = 'image/*';
            input.onchange = (e) => {
                const file = e.target.files[0];
                if (file) {
                    handleFile(file);
                }
            };
            input.click();
        });

        // 处理文件
        function handleFile(file) {
            originalFile = file;
            compressedFile = null;
            
            // 显示预览
            const reader = new FileReader();
            reader.onload = (e) => {
                previewArea.innerHTML = `
                    <div class="preview-box">
                        <h3>原始图片</h3>
                        <img src="${e.target.result}" class="preview-image">
                        <div class="image-info">大小: ${formatFileSize(file.size)}</div>
                        <div class="image-info">类型: ${file.type}</div>
                    </div>
                    <div class="preview-box">
                        <h3>压缩预览</h3>
                        <div class="preview-image" style="display: flex; align-items: center; justify-content: center; background: rgba(255,255,255,0.05);">
                            等待压缩...
                        </div>
                    </div>
                `;
                compressBtn.disabled = false;
                downloadBtn.disabled = true;
            };
            reader.readAsDataURL(file);
        }

        // 压缩图片
        compressBtn.addEventListener('click', async () => {
            if (!originalFile) return;

            compressBtn.disabled = true;
            compressBtn.textContent = '压缩中...';

            try {
                const options = {
                    maxSizeMB: 1,
                    maxWidthOrHeight: Math.max(parseInt(maxWidthInput.value), parseInt(maxHeightInput.value)),
                    useWebWorker: true,
                    quality: parseFloat(qualityInput.value),
                    fileType: originalFile.type
                };

                compressedFile = await imageCompression(originalFile, options);
                
                // 显示压缩结果
                const reader = new FileReader();
                reader.onload = (e) => {
                    const originalSize = originalFile.size;
                    const compressedSize = compressedFile.size;
                    const ratio = ((originalSize - compressedSize) / originalSize * 100).toFixed(1);

                    previewArea.innerHTML = `
                        <div class="preview-box">
                            <h3>原始图片</h3>
                            <img src="${URL.createObjectURL(originalFile)}" class="preview-image">
                            <div class="image-info">大小: ${formatFileSize(originalSize)}</div>
                            <div class="image-info">类型: ${originalFile.type}</div>
                        </div>
                        <div class="preview-box">
                            <h3>压缩预览</h3>
                            <img src="${e.target.result}" class="preview-image">
                            <div class="image-info">大小: ${formatFileSize(compressedSize)}</div>
                            <div class="image-info">类型: ${compressedFile.type}</div>
                            <div class="compression-ratio">压缩率: ${ratio}%</div>
                        </div>
                    `;
                    downloadBtn.disabled = false;
                    toast.success('压缩完成');
                };
                reader.readAsDataURL(compressedFile);
            } catch (error) {
                toast.error('压缩失败，请重试');
                console.error('压缩错误:', error);
            } finally {
                compressBtn.disabled = false;
                compressBtn.textContent = '开始压缩';
            }
        });

        // 下载压缩后的图片
        downloadBtn.addEventListener('click', () => {
            if (!compressedFile) return;

            const link = document.createElement('a');
            link.href = URL.createObjectURL(compressedFile);
            link.download = `compressed_${originalFile.name}`;
            link.click();
            toast.success('开始下载');
        });

        // 格式化文件大小
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 B';
            const k = 1024;
            const sizes = ['B', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }
    </script>
</body>
</html> 